@include('admin::layouts.header')
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>栏目管理</legend>
    </fieldset>
	<blockquote class="layui-elem-quote">
        <a href="{{route('blog_admin.column.create')}}" class="layui-btn layui-btn-sm">添加</a>
        <a class="layui-btn layui-btn-normal layui-btn-sm"  onclick="openAll();">展开或折叠全部</a>
    </blockquote>
    <table class="layui-table" id="treeTable" lay-filter="treeTable"></table>
</div>
@include('admin::layouts.footer')
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="@{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="status" @{{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="sort">
    <input name="@{{d.id}}" data-id="@{{d.id}}" class="list_order layui-input" value=" @{{d.sort}}" size="10"/>
</script>
<script type="text/html" id="action">
	 <a href="/blog_admin/column/@{{d.id}}/create_sublevel" class="layui-btn layui-btn-xs layui-btn-warm">添加子级</a>
    <a href="/blog_admin/column/@{{d.id}}/edit" class="layui-btn layui-btn-xs">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
var editObj=null,ptable=null,treeGrid=null,tableId='treeTable',layer=null;
    layui.config({
        base: "{!! config('admin.admin_url') !!}/plugins/layui/extend/"
    }).extend({
        treeGrid:'treeGrid'
    }).use(['jquery','treeGrid','layer','form'], function(){
        var $=layui.jquery;
        treeGrid = layui.treeGrid;
        layer=layui.layer;
		form = layui.form;
        ptable=treeGrid.render({
            id:tableId
            ,elem: '#'+tableId
            ,idField:'id'
            ,url:"{{route('blog_admin.column.data')}}"
            ,cellMinWidth: 100
            ,treeId:'id'
            ,treeUpId:'pid'
            ,treeShowName:'title'
            ,height:'full-140'
            ,isFilter:false
            ,iconOpen:true
            ,isOpenDefault:true
            ,cols: [[
                {field: 'id', title: '编号', width: 70, fixed: true},
                {field: 'title', title: '权限名称', width: 200},
                {field: 'status',align: 'center',title: '菜单状态', width: 150,toolbar: '#status'},
                {field: 'sort',align: 'center', title: '排序', width: 80, templet: '#sort'},
                {width: 300,align: 'center',title: '操作', toolbar: '#action'}
            ]]
            ,page:false
        });
        treeGrid.on('tool('+tableId+')',function (obj) {
			var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定要删除吗！', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{{route('blog_admin.column.destroy')}}",{_method:'DELETE',id:data.id},function(res){ 
                        layer.close(loading);
						if(res.status == 1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg(res.error,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
		form.on('switch(status)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var status = obj.elem.checked===true?1:0;
            $.post('/blog_admin/column/'+id+'/status',{_method:'PUT',status:status},function (res) {
                layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
            })
        });
		$('body').on('blur','.list_order',function() {
		   loading =layer.load(1, {shade: [0.1,'#fff']}); 
           var id = $(this).attr('data-id');
           var sort = $(this).val();
           $.post('/blog_admin/column/'+id+'/sorts',{_method:'PUT',sort:sort},function(res){
                layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
           })
        })
    });
function openAll() {
    var treedata=treeGrid.getDataTreeList(tableId);
    treeGrid.treeOpenAll(tableId,!treedata[0][treeGrid.config.cols.isOpen]);
}
</script>